const {
  honorGetListApi,
  fileGetAliOssUrlPathApi
} = require('../../api/index')
Page({

  /**
   * 页面的初始数据
   */
  data: {
    currentPage: 1,
    lawyerId: undefined as any,
    honorList: [] as any,
    aliOssUrlPath: ""
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    this.setData({
      lawyerId: options.lawyerId
    })
    fileGetAliOssUrlPathApi().then(res => {
      this.setData({
        aliOssUrlPath: res.data.aliOssUrlPath
      })
      this.getList()
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {
    this.setData({
      currentPage: this.data.currentPage += 1
    })
    this.getList()

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  },
  getList() {
    const { currentPage, lawyerId } = this.data
    honorGetListApi({
      pageNum: currentPage,
      pageSize: 6,
      lawyerId
    }).then(res => {
      wx.hideNavigationBarLoading()
      res.data.list.forEach(t => {
        t.content.replace(/<img [^>]*src=['"]([^'"]+)[^>]*>/, (match, capture) => {
          t.img = capture.replace('{path}', this.data.aliOssUrlPath)
        });
      });
      if (res.data.list.length) {
        this.setData({
          honorList: this.data.honorList.concat(res.data.list)
        })
      } else {
        wx.showToast({
          icon: 'none',
          title: '没有更多数据了'
        })
      }
    })
  }
})